package com.youlu.campus.admin.course.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.course.dto.*;
import com.youlu.campus.admin.course.service.CourseOrderService;
import com.youlu.campus.admin.course.service.ThirdOrderService;
import com.youlu.campus.admin.course.vo.ThirdCourseOrderVo;
import com.youlu.campus.admin.course.vo.UserCourseOrderDetailVo;
import com.youlu.campus.admin.utils.AuthUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : CourseOrderController
 * @description : [课程订单，课程订单]
 * @createTime : [2022/5/20 22:30]
 */
@Slf4j
@RestController
public class CourseOrderController {
    @Autowired
    private CourseOrderService courseOrderService;
    @Autowired
    private ThirdOrderService thirdOrderService;
    @Autowired
    private RoleService roleService;

    /**
     * 课程订单视频列表
     * 课程视频订单
     * @param request 课程订单课程列表查询请求类
     * @return
     */
    @PostMapping("/course-order/list")
    public QResult list(@Validated @RequestBody UserCourseOrderSearchDTO request) {
        log.info("CourseOrderController,list,request param:{}", JSON.toJSONString(request));
        String userId = AuthUtils.getUserId();
        request.setUserId(userId);
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> role.getMobileDesensitization()).count();
        if(count>0){
            request.setMobileDesensitization(Boolean.TRUE);
            log.info("【手机号脱敏】当前用户 ->{} 手机号脱敏",AuthUtils.getLoginName());
        }
        long countMobileHide = roleList.stream().filter(role -> role.getMobileHide()).count();
        if(countMobileHide>0){
            log.info("【手机号隐藏】当前用户 ->{} 手机号隐藏",AuthUtils.getLoginName());
            request.setMobileHide(Boolean.TRUE);
        }
        Page<UserCourseOrderDetailVo> data = courseOrderService.list(request);
        log.info("CourseOrderController,list,response:{}", data);

        return QResult.success(data);
    }

    /**
     * 课程订单退款申请
     * 课程视频订单退款申请
     * @param request 课程订单退款申请
     * @return
     */
    @PostMapping("/course-order/refund/apply")
    public QResult refundApply(@Validated @RequestBody CourseOrderRefundApplyDTO request) {
        log.info("CourseOrderController,refundApply,request param:{}", JSON.toJSONString(request));
        boolean data = courseOrderService.refundApply(request);
        log.info("CourseOrderController,refundApply,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 课程订单退款申请操作
     * 课程视频退款操作
     * @param request 课程订单退款申请操作
     * @return
     */
    @PostMapping("/course-order/refund/op")
    public QResult refundOp(@Validated @RequestBody CourseOrderRefundOpDTO request) {
        log.info("CourseOrderController,refundOp,request param:{}", JSON.toJSONString(request));
        boolean data = courseOrderService.refundOp(request);
        log.info("CourseOrderController,refundOp,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 三方课程视频
     * 三方视频课程订单列表
     * @param request 三方课程视频
     * @return
     */
    @PostMapping("/third-course-order/list")
    public QResult thirdCourseList(@Validated @RequestBody ThirdCourseVideoSearchDTO request) {
        log.info("CourseOrderController,ThirdCourseVideoSearchDTO,request param:{}", JSON.toJSONString(request));
        Page<ThirdCourseOrderVo> data = thirdOrderService.getOrderPageInfo(request);
        return QResult.success(data);
    }

    /**
     * 三方课程视频订单统计
     * 三方视频课程订单统计
     * @param request 三方课程订单统计
     * @return
     */
    @PostMapping("/third-course-order/statistics")
    public QResult thirdStatistics(@Validated @RequestBody ThirdCourseStatisticsDTO request) {
        log.info("CourseOrderController,ThirdCourseStatisticsDTO,request param:{}", JSON.toJSONString(request));
        return QResult.success(thirdOrderService.getOrderStatistics(request));
    }

    /**
     * 三方课程视频订单导出
     * 三方视频课程订单导出
     * @param request 三方课程视频订单导出
     * @return
     */
    @PostMapping("/third-course-order/export")
    public QResult exportThirdOrder(@Validated @RequestBody ThirdCourseVideoSearchDTO request, HttpServletResponse response) {
        log.info("CourseOrderController,exportThirdOrder,request param:{}", JSON.toJSONString(request));
        thirdOrderService.exportThirdOrder(request,response, AuthUtils.getLoginName(), AuthUtils.getUserId());
        return QResult.success(null);
    }
}